using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;

namespace JNPF.Basics.Logic
{
    public class CustomerneedrecordBll
    {
        private CustomerneedrecordIService service = new CustomerneedrecordService();
        private CustomerneedIService needService = new CustomerneedService();

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomerneedrecordEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerneedrecordEntity>> GetPageList(CustomerneedrecordListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerneedrecordEntity entity)
        {
            try
            {
                CustomerneedEntity customerneedEntity = await needService.GetInfo(entity.CustomerNeedId);

                if (customerneedEntity == null)
                {
                    return 0;
                }

                if (customerneedEntity.Type == 0)
                {
                    customerneedEntity.Type = 1;
                }

                CustomerneedrecordEntity customerneedrecord = await service.GetInfo(entity.CustomerNeedId, UserProvider.Instance.Get().UserId);
                if (customerneedrecord == null)
                {
                    entity.CreatorTime = DateTime.Now;
                    entity.LastModifyTime = DateTime.Now; ;
                    entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                    entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                    entity.CreatorUser = UserProvider.Instance.Get().UserName;
                    entity.EnabledMark = 1;
                    entity.DeleteMark = 0;
                    entity.AccountManagerUserId = UserProvider.Instance.Get().UserId;

                    entity.AccountManager = UserProvider.Instance.Get().UserName;

                    if (!string.IsNullOrEmpty(UserProvider.Instance.Get().OrganizeId))
                    {
                        OrganizeEntity organizeEntity = await new OrganizeService().GetInfo(UserProvider.Instance.Get().OrganizeId);
                        if (organizeEntity != null)
                        {
                            entity.BelonBank = organizeEntity.FullName;
                        }

                    }
                    await needService.Update(customerneedEntity);
                    return await service.Create(entity);
                }
                else
                {
                    customerneedrecord.LastModifyTime = DateTime.Now; ;
                    customerneedrecord.LastModifyUserId = UserProvider.Instance.Get().UserId;

                    customerneedEntity.LastModifyTime = DateTime.Now; ;
                    customerneedEntity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                    

                    await needService.Update(customerneedEntity);
                    return await service.Update(customerneedrecord);
                }
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, CustomerneedrecordEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerneedrecordEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<CustomerneedrecordEntity>> GetList(CustomerneedrecordListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}